package leecode;

import java.util.Queue;
import java.util.Stack;

public class Leetcode25 {

    public int[] spiralOrder(int[][] matrix) {
        if(matrix.length==0){
            return new int[0];
        }
        int a=matrix.length-1;
        int b=matrix[0].length-1;
        int []arr=new int[(a+1)*(b+1)];
        int k=0;
        int c=0;//左
        int d=0;//上

        while (arr.length!=k) {
            for (int i = c; i <=b ; i++,k++) {//从左向右打印行
                arr[k]=matrix[d][i];
            }
            if(arr.length==k){
                break;
            }
            d++;
            for (int i = d; i <=a; i++,k++) {//从上到下打印列
                arr[k]=matrix[i][b];
            }
            if(arr.length==k){
                break;
            }
            b--;
            for (int i = b; i >=c ; i--,k++) {//从右向左打印行
                arr[k]=matrix[a][i];
            }
            a--;
            if(arr.length==k){
                break;
            }
            for (int i = a; i >=d ; i--,k++) {//从下到上打印列
                arr[k]=matrix[i][c];
            }
            c++;
            if(arr.length==k){
                break;
            }




        }
        return arr;
    }

    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> stack=new Stack();
        int k=0;
        for (int i:pushed) {
            stack.push(i);
            while (!stack.empty() && stack.peek() == popped[k]) {
                stack.pop();
                k++;
            }
        }
        if(k==popped.length){
            return true;
        }
        return false;
    }

    public static void main(String[] args) {
        Leetcode25 leetcode25=new Leetcode25();
        int arr[][]={{1,2,3,4},{5,6,7,8},{9,10,11,12}};
        leetcode25.spiralOrder(arr);
    }
}
